package adc.app.ui.wicket.util;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.util.string.JavaScriptUtils;

public final class WktUtil {

    private WktUtil() {
        throw new IllegalStateException("This class is not meant to be instantiated, ever");
    }

    @SuppressWarnings("unchecked")
    public static <C extends Component> C getChild(final MarkupContainer container, final String... path) {
        final StringBuilder sbPath = new StringBuilder(path[0]);
        for (int i = 1; i < path.length; i++) {
            sbPath.append(Component.PATH_SEPARATOR).append(path[i]);
        }
        return (C) container.get(sbPath.toString());
    }

    public static void alert(final AjaxRequestTarget target, final CharSequence msg) {
        final String js = new StringBuilder().append("alert('").append(JavaScriptUtils.escapeQuotes(msg)).append("')")
                .toString();
        target.appendJavaScript(js);
    }
}
